package cn.com.zpark.servlet_request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @PackageName: cn.com.zpark.servlet_request
 * @ClassName: ServletRequestDemo02
 * @Description:
 * @author: RZS
 * @date: 2021/11/13  15:14
 */
@WebServlet("/srd2.do")
public class ServletRequestDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        writer.write("<center>");

        // 返回请求头中所有头字段的枚举集合
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            // 获取请求头名字
            String name = names.nextElement();
            // 根据请求头名字获取对应的值
            String value = request.getHeader(name);
            System.out.println(name+": "+value);
            writer.write("<h3>"+name+"： "+value+"</h3>");
        }
        writer.write("</center>");
    }
}
